package com.mxw.算法之美.a7二分查找;

public class a01不存在重复元素 {



    /**
     * 二分查找：
     * 底层依赖于有序的数组
     *
     */
    int binarySearch(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        // 循环推出条件
        while(left <= right) {
            // mid的取值
            int mid = left + (right - left) / 2;
            // 左右边界的更新
            if(nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            }
        }
        return -1;
    }

}
